﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;
using MxWeiXinPF.Common;//Please add references
namespace MxWeiXinPF.DAL
{
    /// <summary>
    /// 数据访问类:wx_shop_coupon
    /// </summary>
    public partial class wx_shop_coupon
    {
        public wx_shop_coupon()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from wx_shop_coupon");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(MxWeiXinPF.Model.wx_shop_coupon model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into wx_shop_coupon(");
            strSql.Append("couponname,money,coupontype,shopid,enough,description,createtime,status,thumb,categoryid,timestart,timeend,numperuser,totalnum,expiredthumb,usedthumb)");
            strSql.Append(" values (");
            strSql.Append("@couponname,@money,@coupontype,@shopid,@enough,@description,@createtime,@status,@thumb,@categoryid,@timestart,@timeend,@numperuser,@totalnum,@expiredthumb,@usedthumb)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@couponname", SqlDbType.VarChar,255),
					new SqlParameter("@money", SqlDbType.Decimal,9),
					new SqlParameter("@coupontype", SqlDbType.Int,4),
					new SqlParameter("@shopid", SqlDbType.Int,4),
					new SqlParameter("@enough", SqlDbType.Decimal,9),
					new SqlParameter("@description", SqlDbType.Text),
					new SqlParameter("@createtime", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@thumb", SqlDbType.VarChar,255),
					new SqlParameter("@categoryid", SqlDbType.Int,4),
					new SqlParameter("@timestart", SqlDbType.DateTime),
					new SqlParameter("@timeend", SqlDbType.DateTime),
					new SqlParameter("@numperuser", SqlDbType.Int,4),
					new SqlParameter("@totalnum", SqlDbType.Int,4),
                    new SqlParameter("@expiredthumb", SqlDbType.VarChar,255),
                    new SqlParameter("@usedthumb", SqlDbType.VarChar,255)
                                        };
            parameters[0].Value = model.couponname;
            parameters[1].Value = model.money;
            parameters[2].Value = model.coupontype;
            parameters[3].Value = model.shopid;
            parameters[4].Value = model.enough;
            parameters[5].Value = model.description;
            parameters[6].Value = model.createtime;
            parameters[7].Value = model.status;
            parameters[8].Value = model.thumb;
            parameters[9].Value = model.categoryid;
            parameters[10].Value = model.timestart;
            parameters[11].Value = model.timeend;
            parameters[12].Value = model.numperuser;
            parameters[13].Value = model.totalnum;
            parameters[14].Value = model.expiredthumb;
            parameters[15].Value = model.usedthumb;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(MxWeiXinPF.Model.wx_shop_coupon model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update wx_shop_coupon set ");
            strSql.Append("couponname=@couponname,");
            strSql.Append("money=@money,");
            strSql.Append("coupontype=@coupontype,");
            strSql.Append("shopid=@shopid,");
            strSql.Append("enough=@enough,");
            strSql.Append("description=@description,");
            strSql.Append("createtime=@createtime,");
            strSql.Append("status=@status,");
            strSql.Append("thumb=@thumb,");
            strSql.Append("categoryid=@categoryid,");
            strSql.Append("timestart=@timestart,");
            strSql.Append("timeend=@timeend,");
            strSql.Append("numperuser=@numperuser,");
            strSql.Append("totalnum=@totalnum,");
            strSql.Append("expiredthumb=@expiredthumb,");
            strSql.Append("usedthumb=@usedthumb");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@couponname", SqlDbType.VarChar,255),
					new SqlParameter("@money", SqlDbType.Decimal,9),
					new SqlParameter("@coupontype", SqlDbType.Int,4),
					new SqlParameter("@shopid", SqlDbType.Int,4),
					new SqlParameter("@enough", SqlDbType.Decimal,9),
					new SqlParameter("@description", SqlDbType.Text),
					new SqlParameter("@createtime", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@thumb", SqlDbType.VarChar,255),
					new SqlParameter("@categoryid", SqlDbType.Int,4),
					new SqlParameter("@timestart", SqlDbType.DateTime),
					new SqlParameter("@timeend", SqlDbType.DateTime),
					new SqlParameter("@numperuser", SqlDbType.Int,4),
					new SqlParameter("@totalnum", SqlDbType.Int,4),
                    new SqlParameter("@expiredthumb", SqlDbType.VarChar,255),
                    new SqlParameter("@usedthumb", SqlDbType.VarChar,255),
					new SqlParameter("@id", SqlDbType.Int,4)
                                        };
            parameters[0].Value = model.couponname;
            parameters[1].Value = model.money;
            parameters[2].Value = model.coupontype;
            parameters[3].Value = model.shopid;
            parameters[4].Value = model.enough;
            parameters[5].Value = model.description;
            parameters[6].Value = model.createtime;
            parameters[7].Value = model.status;
            parameters[8].Value = model.thumb;
            parameters[9].Value = model.categoryid;
            parameters[10].Value = model.timestart;
            parameters[11].Value = model.timeend;
            parameters[12].Value = model.numperuser;
            parameters[13].Value = model.totalnum;
            parameters[14].Value = model.expiredthumb;
            parameters[15].Value = model.usedthumb;
            parameters[16].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wx_shop_coupon ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wx_shop_coupon ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MxWeiXinPF.Model.wx_shop_coupon GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,couponname,money,coupontype,shopid,enough,description,createtime,status,thumb,categoryid,timestart,timeend,numperuser,totalnum,expiredthumb,usedthumb from wx_shop_coupon ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            MxWeiXinPF.Model.wx_shop_coupon model = new MxWeiXinPF.Model.wx_shop_coupon();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MxWeiXinPF.Model.wx_shop_coupon DataRowToModel(DataRow row)
        {
            MxWeiXinPF.Model.wx_shop_coupon model = new MxWeiXinPF.Model.wx_shop_coupon();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["couponname"] != null)
                {
                    model.couponname = row["couponname"].ToString();
                }
                if (row["money"] != null && row["money"].ToString() != "")
                {
                    model.money = decimal.Parse(row["money"].ToString());
                }
                if (row["coupontype"] != null && row["coupontype"].ToString() != "")
                {
                    model.coupontype = int.Parse(row["coupontype"].ToString());
                }
                if (row["shopid"] != null && row["shopid"].ToString() != "")
                {
                    model.shopid = int.Parse(row["shopid"].ToString());
                }
                if (row["enough"] != null && row["enough"].ToString() != "")
                {
                    model.enough = decimal.Parse(row["enough"].ToString());
                }
                if (row["description"] != null)
                {
                    model.description = row["description"].ToString();
                }
                if (row["createtime"] != null && row["createtime"].ToString() != "")
                {
                    model.createtime = DateTime.Parse(row["createtime"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["thumb"] != null)
                {
                    model.thumb = row["thumb"].ToString();
                }
                if (row["expiredthumb"] != null)
                {
                    model.expiredthumb = row["expiredthumb"].ToString();
                }
                if (row["usedthumb"] != null)
                {
                    model.usedthumb = row["usedthumb"].ToString();
                }
                if (row["categoryid"] != null && row["categoryid"].ToString() != "")
                {
                    model.categoryid = int.Parse(row["categoryid"].ToString());
                }
                if (row["timestart"] != null && row["timestart"].ToString() != "")
                {
                    model.timestart = DateTime.Parse(row["timestart"].ToString());
                }
                if (row["timeend"] != null && row["timeend"].ToString() != "")
                {
                    model.timeend = DateTime.Parse(row["timeend"].ToString());
                }
                if (row["numperuser"] != null && row["numperuser"].ToString() != "")
                {
                    model.numperuser = int.Parse(row["numperuser"].ToString());
                }
                if (row["totalnum"] != null && row["totalnum"].ToString() != "")
                {
                    model.totalnum = int.Parse(row["totalnum"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,couponname,money,coupontype,shopid,enough,description,createtime,status,thumb,categoryid,timestart,timeend,numperuser,totalnum,expiredthumb,usedthumb ");
            strSql.Append(" FROM wx_shop_coupon ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,couponname,money,coupontype,shopid,enough,description,createtime,status,thumb,categoryid,timestart,timeend,numperuser,totalnum,expiredthumb,usedthumb ");
            strSql.Append(" FROM wx_shop_coupon ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM wx_shop_coupon ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(string.Concat(new object[] { " select  * from wx_shop_coupon where 1=1 " }));
            if (strWhere.Trim() != "")
            {
                builder.Append("  " + strWhere);
            }
            //if (filedOrder.Trim() != "")
            //{
            //    builder.Append(" order by " + filedOrder);
            //}
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(builder.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, builder.ToString(), filedOrder));
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from wx_shop_coupon T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "wx_shop_coupon";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

